package com.xyu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.util.StringUtils;
import com.xyu.gulimall.product.dao.BrandDao;
import com.xyu.gulimall.product.dao.CategoryDao;
import com.xyu.gulimall.product.entity.BrandEntity;
import com.xyu.gulimall.product.entity.CategoryEntity;
import com.xyu.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xyu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.xyu.gulimall.product.service.CategoryBrandRelationService;
import com.xyu.common.utils.PageUtils;
import com.xyu.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌分类关联
 *
 * @author wangyufei
 * @email 2070522113@qq.com
 * @date 2021-05-03 21:26:59
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Resource
    private BrandDao brandDao;
    @Resource
    private CategoryDao categoryDao;

    /**
     * 14、获取分类关联的品牌
     *请求地址： /product/categorybrandrelation/brands/list
     */

    @GetMapping("/brands/list")
    public R categoryBrandRelation(@RequestParam(value = "catId",required = true) Long catId){
        List<BrandEntity> brandEntities =  categoryBrandRelationService.getBrandId(catId);
        List<BrandVo> collect = brandEntities.stream().map(item -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);
    }




    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     * 修改回显信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        //获取品牌id
        Long brandId = categoryBrandRelation.getBrandId();
        //获取商品
        Long catelogId = categoryBrandRelation.getCatelogId();
        //根据id查询品牌表
        BrandEntity brandEntity = brandDao.selectById(brandId);
        //根据id查询商品表
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        //获取品牌名
        categoryBrandRelation.setBrandName(brandEntity.getName());
        //获取商品名
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        //保存
        categoryBrandRelationService.save(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 获取当前品牌关联的所有分类列表
     */
    @GetMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R cateloglist(@RequestParam("brandId")Long brandId){
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId)
        );

        return R.ok().put("data", data);
    }



}
